<template>
	<div class="">
		<el-dialog class="shop-config-dialog" title="打开/关闭商城" :visible.sync="dialogVisible" width="500px" :before-close="handleClose"
			:close-on-click-modal="false" center>
			<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
				<el-form-item label="商城状态" prop="mall_switch">
					<el-switch v-model="ruleForm.mall_switch" active-text="打开" inactive-text="关闭"></el-switch>
				</el-form-item>
				<el-form-item v-if="ruleForm.mall_switch" label="商城key" prop="key">
					<el-input v-model="ruleForm.key" clearable placeholder="请输入商城key"></el-input>
				</el-form-item>
				<el-form-item v-if="ruleForm.mall_switch" label="商城secret" prop="secret">
					<el-input v-model="ruleForm.secret" clearable placeholder="请输入商城secret"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="handleClose">关闭</el-button>
				<el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import Api from '@/api/pages/wuye/wuye.js';
	export default {
		name: 'shop-config',
		props: {
			data: Object
		},
		data() {
			return {
				dialogVisible: true,
				ruleForm: {
					wuye_id: '',
					mall_switch: null,
					key: '',
					secret: '',
				},
				rules: {
					mall_switch: [{
						required: true,
						message: '不能为空',
						trigger: 'change'
					}],
					key: [{
						required: true,
						message: '不能为空',
						trigger: 'blur'
					}],
					secret: [{
						required: true,
						message: '不能为空',
						trigger: 'blur'
					}],
				},
			};
		},
		mounted() {
			// console.log(this.data)
			this.ruleForm = {
				wuye_id: this.data.id,
				mall_switch: this.data.mall_switch,
				key: this.data.supply_chain_api_key,
				secret: this.data.supply_chain_api_secret,
			}
		},
		methods: {
			handleClose() {
				this.$refs['ruleForm'].resetFields();
				this.$emit('close');
			},
			save() {
				let data = {};
				data['wuye_id'] = this.ruleForm.wuye_id;
				data['mall_switch'] = this.ruleForm.mall_switch ? true : false;
				data['key'] = this.ruleForm.key;
				data['secret'] = this.ruleForm.secret;
				// console.log(data)
				Api.updMallConfig(data).then(res => {
					if (res.state) {
						this.$message({
							message: res.msg,
							type: 'success'
						});
						this.$parent.getData();
						this.$emit('close');
					} else {
						this.$message(res.msg);
					}
				});
			},
			
			submitForm(formName) {
				this.$refs[formName].validate(valid => {
					if (valid) {
						this.save()
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},

		}
	};
</script>
<style lang="scss" scoped>

</style>

<style lang="scss">
	.shop-config-dialog {
		.el-dialog__body {
			padding: 20px;
		}
	}
</style>
